package com.user.basic.analyze.register;

import com.entity.analyze.RegisterType;
import org.springframework.stereotype.Service;

import java.lang.ref.Reference;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class MonthRegisterAnalyzeServiceImpl extends RegisterAnalyzeServiceImpl {
    public MonthRegisterAnalyzeServiceImpl(RegisterAnalyzeMapper registerAnalyzeMapper) {
        super(registerAnalyzeMapper);
    }

    @Override
    protected Map<String, Integer> transform(Reference<ConcurrentHashMap<String, List<Date>>> cache) {
        if (Objects.requireNonNull(cache.get()).size() >0){
            Map<String, Integer> countMap = new HashMap<>();
            cache.get().forEach((k, v) -> v.forEach(date -> countMap.compute(((1900+date.getYear())+"-"
                            +(date.getMonth()+1)),
                    RegisterAnalyzeServiceImpl::apply)
            ));
            return countMap;
        }
        return null;
    }

    @Override
    protected void save(Map<Object, Integer> transform) {
        save(transform, RegisterType.Month);
    }
}
